package com.jml.manage;

import com.jml.entity.OrderEntity;
import com.jml.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;


@Component
public class OrderManage {
    @Autowired
    private OrderMapper orderMapper;


    @Transactional
    public String insertOrder(String orderId) {
        OrderEntity newOrder = createOrder(orderId);
        // 1.向订单数据库表插入数据
        int result = orderMapper.addOrder(newOrder);
        if (result < 0) {
            return "fail";
        }
        return "success";
    }

    public OrderEntity createOrder(String orderId) {
        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setName("jml-name");
        orderEntity.setOrderCreatetime(new Date());
        orderEntity.setOrderMoney(300d);
        // 状态为 未支付
        orderEntity.setOrderState(0);
        Long commodityId = 30L;
        // 商品id
        orderEntity.setCommodityId(commodityId);
        orderEntity.setOrderId(orderId);
        return orderEntity;
    }
}
